package com.wk.springboot.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.commons.lang.StringUtils;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;

/**
 * @Classname EsClientUtils
 * @Description TODO
 * @Date 2020/5/28 20:00
 * @Author by weikai
 */
public class EsClientUtils {

  public static TransportClient createClient() throws UnknownHostException {
    Settings.Builder builder = Settings.builder();
    builder.put("cluster.name", "esCluster");
    builder.put("xpack.security.transport.ssl.enabled", false);
    if (StringUtils.isNotBlank("") && StringUtils.isNotBlank("")) {
      builder.put("xpack.security.user", "" + ":" + "");
    }
    //自动嗅探整个ES集群节点
    builder.put("client.transport.sniff", true);
    /*builder.put("client.transport.ping_timeout", 300);*/
    TransportClient client = new PreBuiltXPackTransportClient(builder.build()).addTransportAddress(
        new InetSocketTransportAddress(InetAddress.getByName("172.16.11.57"), 9300));
    return client;
  }
}
